package com.care.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.care.entity.Doctor;
import com.care.entity.Knowledge;
import com.care.service.KnowledgeServiceImpl;
import com.care.ui.KnowledgePage;

@Controller
public class KnowledgeController {
	@Resource
	private KnowledgeServiceImpl serviceImpl;
	/**
	 * 添加医护知识且去重
	 * @param request
	 * @param response
	 * @param knowledge
	 * @param doctor
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("AddKnowledge")
	public String AddKnowledge(HttpServletRequest request,HttpServletResponse response,Knowledge knowledge,Doctor doctor) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		HttpSession session=request.getSession();
		doctor=(Doctor) session.getAttribute("doctor");
		if (this.serviceImpl.UniqueKnowledge(knowledge)) {
			this.serviceImpl.AddKnowledgeByDoctor(knowledge,doctor);
			response.getWriter().write("<script> alert('添加成功');window.location='blog.jsp'</script>");
		}
		else {
			response.getWriter().write("<script> alert('主题相同,请重新添加');window.location='addKnowledge.jsp'</script>");
		}		
		return null;		
	}
	/**
	 * 医护知识分页
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("KnowledgePage")
	public String KnowledgePage(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();		
		//医护知识分页
		String knumber=request.getParameter("kPageNum");	
		int kPageNum=0;
		if (knumber == null || knumber.equals("")) {
			kPageNum=1;				
		}
		else {
			kPageNum=Integer.parseInt(knumber);
		}	
		List<Knowledge> knowledges=this.serviceImpl.FindKnowledgeByPage(kPageNum,2);
		int countKnowledge=this.serviceImpl.CountKnowledge();
		KnowledgePage<Knowledge> knowledgePage=new KnowledgePage<Knowledge>(kPageNum,2);
		knowledgePage.setList(knowledges);
		knowledgePage.setTotalcount(countKnowledge);
		session.setAttribute("knowledgePage", knowledgePage);
		return "/index.jsp";
		
	}
	@RequestMapping("blog")
	public String blog(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		String number=request.getParameter("pageNum");
		int pageNum=0;
		if (number== null || number.equals("")) {
			pageNum=1;			
		}
		else {
			pageNum=Integer.parseInt(number);			
		}
		List<Knowledge> knowledges=this.serviceImpl.FindKnowledgeByPage(pageNum,4);
		int countKnowledge=this.serviceImpl.CountKnowledge();
		KnowledgePage<Knowledge> blog=new KnowledgePage<Knowledge>(pageNum,4);
		blog.setList(knowledges);
		blog.setTotalcount(countKnowledge);
		session.setAttribute("blog", blog);
		return "/blog.jsp";
	}
	@RequestMapping("singleblog")
	public String singleblog(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		String number=request.getParameter("pageNum");
		int pageNum=0;
		if (number== null || number.equals("")) {
			pageNum=1;			
		}
		else {
			pageNum=Integer.parseInt(number);			
		}
		List<Knowledge> knowledges=this.serviceImpl.FindKnowledgeByPage(pageNum,1);
		int countKnowledge=this.serviceImpl.CountKnowledge();
		KnowledgePage<Knowledge> singleblog=new KnowledgePage<Knowledge>(pageNum,1);
		singleblog.setList(knowledges);
		singleblog.setTotalcount(countKnowledge);
		session.setAttribute("singleblog", singleblog);
		return "/blog-single.jsp";
	}
	

}
